package com.hk.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hk.domain.Business;
import com.hk.domain.Clue;
import com.hk.mapper.BusinessMapper;
import com.hk.mapper.ClueMapper;
import com.hk.mapper.ContractMapper;
import com.hk.mapper.IndexMapper;
import com.hk.service.IndexService;
import com.hk.vo.BusinessChangeLevelVo;
import com.hk.vo.ClueChangeLevelVo;
import com.hk.vo.IndexClueVo;
import com.hk.vo.TodayInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class IndexServiceImpl implements IndexService {
    public static final Integer TRUE=0;
    public static final Integer FALSE=1;

    // 获取今天的开始和结束时间
    LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
    LocalDateTime endOfDay = LocalDate.now().plusDays(FALSE).atStartOfDay();

    @Autowired
    IndexMapper indexMapper;

    private final ClueMapper clueMapper;
    private final BusinessMapper businessMapper;
    private ContractMapper contractMapper;

    @Override
    public TodayInfo todayIndex() {
        //今日线索
        Long todayCluesNum = clueMapper.selectCount(Wrappers.<Clue>lambdaQuery().
                between(Clue::getCreateTime, startOfDay, endOfDay).
                eq(Clue::getAuthenticity, TRUE));
        //今日商机
        Long todayBusinessNum = businessMapper.selectCount(Wrappers.<Business>lambdaQuery().
                between(Business::getCreateTime, startOfDay, endOfDay).
                eq(Business::getAuthenticity, TRUE));
        //todo
        //今日合同
        //今日销售额
        TodayInfo todayInfo = new TodayInfo();
        todayInfo.setTodayBusinessNum(todayCluesNum);
        todayInfo.setTodayBusinessNum(todayBusinessNum);
        todayInfo.setTodayContractNum(10L);
        todayInfo.setTodaySalesAmount(100.0);
        return todayInfo ;
    }

    @Override
    public IndexClueVo queryClue(LocalDateTime star, LocalDateTime end) {
        //总线索
        Long cluesNums = clueMapper.selectCount(Wrappers.<Clue>lambdaQuery().between(Clue::getCreateTime, star, end));
        //有效线索
        Long effectiveCluesNums = clueMapper.selectCount(Wrappers.<Clue>lambdaQuery().
                between(Clue::getCreateTime, star, end).eq(Clue::getAuthenticity, TRUE));
        //商机数
        Long businessNums = businessMapper.selectCount(Wrappers.<Business>lambdaQuery()
                .between(Business::getCreateTime, star, end));
        //todo
        //客户数
        IndexClueVo vo = new IndexClueVo();
        vo.setCluesNums(cluesNums);
        vo.setEffectiveCluesNums(effectiveCluesNums);
        vo.setBusinessNums(businessNums);
        vo.setContractNums(2L);
        return vo;
    }

    //商机转化龙虎榜
    @Override
    public List<BusinessChangeLevelVo> queryBusinessChangeStatic(LocalDateTime star, LocalDateTime end) {
      List<BusinessChangeLevelVo> vo=indexMapper.queryBusinessChangeStatic(star,end);
        Long count = businessMapper.selectCount(null);
        vo.stream().forEach((i)->{
            Integer num = i.getNum();
           Long radio=(num/count)*100;
            i.setRadio(radio.intValue());
      });
        return vo;
    }

    //线索转化龙虎榜
    @Override
    public List<ClueChangeLevelVo> queryClueChangeStatic(LocalDateTime star, LocalDateTime end) {
       List<ClueChangeLevelVo> vo=indexMapper.queryClueChangeStatic(star,end);
        Long count = clueMapper.selectCount(Wrappers.<Clue>lambdaQuery()
                .eq(Clue::getStatus,0));
        vo.stream().forEach((i)->{
            Integer num = i.getNum();
            double radio=((double)num/count)*100;
            i.setRadio(radio);
        });
        return vo;
    }


}
